﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace JzSport.Model
{
    public class ActivityMemberRepository
    {
        private JzSportDbContext _dbContext;

        public ActivityMemberRepository(JzSportDbContext dbContext)
        {
            _dbContext = dbContext;
        }

        public void Create(ActivityMember member)
        {
            _dbContext.Set<ActivityMember>().Add(member);
        }

        public ActivityMember Get(Guid activityId, int userId)
        {
            return _dbContext.Set<ActivityMember>().Where(p => p.ActivityId == activityId && p.UserId == userId)
                .SingleOrDefault();
        }

        public void Delete(ActivityMember member)
        {
            _dbContext.Entry<ActivityMember>(member).State = System.Data.EntityState.Deleted;
        }

        public List<Data.ActivityMember> GetList(Guid activityId)
        {
            return _dbContext.Set<ActivityMember>().Where(p => p.ActivityId == activityId)
                .Join(_dbContext.Set<Account>(),
                l => l.UserId,
                r => r.Id,
                (l, r) => new Data.ActivityMember() { Id = r.Id, 
                    AvatarUrl = r.AvatarUrl, NickName = r.NickName,
                    Type = l.Type, 
                    JoinTime = l.JoinTime,
                    Sex = r.Sex,
                    City = r.City})
                .OrderBy(p => p.JoinTime)
                .ToList();
        }
    }
}
